import 'package:flutter/material.dart';
import 'package:flutter_audit_app/res/strings.dart';

///  项目名称：flutter_audit_app
///  Created by leon on 2020/10/22 .

///  自定义加载对话框
///

enum ProgressHUDType {
  success,
  failure,
  loading,
  none,
}

typedef OnDismiss = Function(BuildContext context);

class PSYProgressHUD extends StatelessWidget {
  //对话框类型
  final ProgressHUDType type;

  final OnDismiss _onDismiss;

  //子布局
  final Widget child;

  //是否显示对话框
  final bool _isShow;

  //对话框文字
  final String label;

  PSYProgressHUD({
    Key key,
    @required this.type,
    bool isShow = true,
    @required this.child,
    OnDismiss onDismiss,
    this.label = Strings.loadingTxt,
  })  : _isShow = isShow,
        _onDismiss = onDismiss,
        super(key: key);

  @override
  Widget build(BuildContext context) {
    _onDismiss(context);
    List<Widget> list = [];
    list.add(child);
    if (_isShow) {
      Widget animationWidget;

      if (type == ProgressHUDType.success || type == ProgressHUDType.failure) {
        final childList = <Widget>[];
        if (label.length > 0) {
          childList.add(Image.asset(type == ProgressHUDType.success
              ? 'assets/images/success.png'
              : 'assets/images/error.png'));
          childList.add(SizedBox(height: 10));
          childList.add(Text(label, style: TextStyle(color: Colors.white)));
        } else {
          childList.add(Image.asset(type == ProgressHUDType.success
              ? 'assets/images/success.png'
              : 'assets/images/error.png'));
        }
        animationWidget = Center(
          child: Container(
            padding: EdgeInsets.fromLTRB(30, 10, 30, 10),
            decoration: BoxDecoration(
              color: Colors.black45,
              borderRadius: BorderRadius.circular(10.0),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: childList,
            ),
          ),
        );
      } else if (type == ProgressHUDType.loading) {
        final childList = <Widget>[];
        if (label.length > 0) {
          childList.add(CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation(Colors.white)));
          childList.add(SizedBox(height: 10));
          childList.add(Text(label, style: TextStyle(color: Colors.white)));
        } else {
          childList
              .add(CircularProgressIndicator(backgroundColor: Colors.white));
        }

        animationWidget = Center(
          child: Container(
            padding: EdgeInsets.all(20.0),
            decoration: BoxDecoration(
              color: Colors.black45,
              borderRadius: BorderRadius.circular(10.0),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: childList,
            ),
          ),
        );
      } else {
        animationWidget = Center(
          child: Container(
            padding: EdgeInsets.all(20.0),
            decoration: BoxDecoration(
              color: Colors.black45,
              borderRadius: BorderRadius.circular(10.0),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Text(label, style: TextStyle(color: Colors.white)),
              ],
            ),
          ),
        );
      }

      final model = [
        Opacity(
          child: Container(
            color: Colors.black87,
          ),
          opacity: 0.6,
        ),
        animationWidget
      ];
      list += model;
    }
    return Stack(
      children: list,
    );
  }
}
